Page({
    data: {
        firstCateList: [], //一级分类
        searchText: '', //搜索关键字
        noticeText: '',
        marquee2: {
            speed: 60,
            loop: -1,
            delay: 0,
        },
        bannerList: [],
        visible_notice: true,
        textColorData: '#000000',
        backgroundData: 'transparent',
        capsuleTop: 0, //胶囊距离顶部距离
        capsuleHight: 0, //胶囊的高度
        goodListLoading: false, //商品列表loading
    },
    async onLoad(options) {
        console.log('扫码获取', options.scene);
        if (options.scene) {
            // 绑定关系
            this.request({
                url: 'user/binding',
                method: 'POST',
                data: {
                    pid: options.scene,
                },
            }).then(res => {
                console.log(res);
                wx.showToast({
                    title: res.msg,
                });
            });
        }
        const capsuleObj = wx.getMenuButtonBoundingClientRect();
        this.setData({
            capsuleTop: capsuleObj.top,
            capsuleHight: capsuleObj.height,
        });
        this.getTitleCategory();
        this.getBanner();
    },
    onShow() {
        this.getNotice();
    },
    // 获取轮播
    getBanner() {
        this.request({
            url: 'user/index/banner/index',
        }).then(res => {
            this.setData({
                bannerList: res.data.data,
            });
        });
    },

    // 预览轮播图图片儿
    onPreviewImg(e) {
      
        const { data, current, format } = e.currentTarget.dataset;
        const urls = data.map(item => {
            return this.data.imgHost + item.url;
        });
        wx.previewImage({
            current, // 当前显示图片的http链接
            urls, // 需要预览的图片http链接列表
        });
    },

    // 获取顶级分类(标题)、、、、、、、、、、、、、、、、、、、、、、、、、、、
    getTitleCategory() {
        this.request({
            url: 'common/dataSelect/goodsCategorySelect',
            data: {
                pid: 0,
                is_all: 0,
            },
        }).then(res => {
            res.data.forEach(item => {
                item.tabs = [];
                item.list = [];
                item.tabActive = 0;
                item.listLoading = false;
            });
            this.setData({
                firstCateList: res.data,
            });
            //调取tab切换的分类
            this.getTabCateList();
        });
    },
    //获取tab切换的分类
    getTabCateList() {
        // console.log(this.data.firstCateList);
        this.data.firstCateList.forEach((firstItem, index) => {
            // 获取tab分类、、、、、、、、、、、、、、、、、、、、、、、、、、
            this.request({
                url: 'common/dataSelect/goodsCategorySelect',
                data: {
                    pid: firstItem.id,
                },
            }).then(res => {
                // console.log('tab', res);
                const tabs = res.data[0].children;
                this.setData({
                    [`firstCateList[${index}].tabs`]: tabs,
                });
                // 获取商品列表
                this.setData({
                    goodListLoading: true,
                });
                this.request({
                    url: 'user/index/goods/index',
                    data: {
                        category_id: tabs[0].id,
                        page: 1,
                        limit: 10,
                        search_str: this.data.searchText,
                    },
                })
                    .then(res => {
                        this.setData({
                            [`firstCateList[${index}].list`]: res.data.data,
                        });
                        this.setData({
                            goodListLoading: false,
                        });
                    })
                    .catch(err => {
                        this.setData({
                            goodListLoading: false,
                        });
                    });
            });
        });
    },
    // 点击切换分类选项
    onTabsChange(e) {
        const sort = e.currentTarget.dataset.sort;
        const active = e.detail.value;
        console.log('sort', sort, 'active', active);
        this.setData({
            [`firstCateList[${sort}].tabActive`]: active,
        });
        this.setData({
            [`firstCateList[${sort}].listLoading`]: true,
        });
        console.log();
        this.request({
            url: 'user/index/goods/index',
            data: {
                category_id: this.data.firstCateList[sort].tabs[this.data.firstCateList[sort].tabActive].id,
                page: 1,
                limit: 10,
            },
        })
            .then(res => {
                this.setData({
                    [`firstCateList[${sort}].list`]: res.data.data,
                });
                this.setData({
                    [`firstCateList[${sort}].listLoading`]: false,
                });
            })
            .catch(err => {
                this.setData({
                    [`firstCateList[${sort}].listLoading`]: false,
                });
            });
    },
    // 开启页面分享
    onShareAppMessage(e) {
        console.log(e);
    },
    getNotice() {
        this.request({
            url: 'user/index/gg/detail',
        }).then(res => {
            this.setData({
                noticeText: res.data,
            });
        });
    },
    // 监听页面的滚动
    onPageScroll(e) {
        const sttop = e.scrollTop;
        if (sttop > 1) {
            this.setData({
                backgroundData: '#FCCCCC',
            });
        } else {
            this.setData({
                backgroundData: 'transparent',
            });
        }
    },
    // 回车搜索
    onSubmit(e) {
        this.setData({
            searchText: e.detail.value,
        });
        this.request({
            url: 'user/index/goods/index',
            data: {
                page: 1,
                limit: 10,
                search_str: this.data.searchText,
            },
        }).then(res => {});
    },
    onPullDownRefresh() {
        wx.stopPullDownRefresh();
    },
});
